let db = require("../db/index");


exports.stafflists = (req, res) => {
    var sql =
        `SELECT * FROM staff`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};

// 员工管理添加
exports.stafflistsinsert = (req, res) => {
    console.log(req.body)
    var sql =
        `INSERT INTO staff (name,sex,department,position,phone,accountstatus,onlinestatus,deactive,freeze,del,admin,psw,sets,avators) VALUES('${req.body.name}','${req.body.sex}','${req.body.selectedProvince}','${req.body.selectedCity}','${req.body.phone}','正常','success','停用','冻结','删除','${req.body.admin}','123456','设置','${req.body.imageUrl}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM staff`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 员工管理设置
exports.stafflistssetup = (req, res) => {
    var sql =
        `UPDATE staff SET department='${req.body.selectedProvince}',position='${req.body.selectedCity}',phone='${req.body.phone}',accountstatus='${req.body.accountstatus}',onlinestatus='${req.body.accountstatus=='正常'?'success':req.body.accountstatus=='冻结'?'warning':req.body.accountstatus=='离线'?'info':'error'}',deactive='${req.body.accountstatus=='停用'?'启用':'停用'}',freeze='${req.body.accountstatus=='冻结'?'启用':'冻结'}',psw='${req.body.password}' WHERE staff_id=${req.body.staffID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM staff`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    var sql =
                        `SELECT * FROM staff`;
                    db.query(sql, [], (err, data) => {
                        if (err) {
                            console.log("出错了", err);
                            return res.send("错误" + err.message);
                        }else{
                            res.send(data);
                        }
                    });
                }
            });
        }
    });
};
// 员工管理停用
exports.stafflistsdeactive = (req, res) => {
    var sql =
        `UPDATE staff SET accountstatus='${req.body.accountstatus}',onlinestatus='${req.body.onlinestatus}',deactive='${req.body.accountstatus=='停用'?'启用':'停用'}',freeze='${req.body.accountstatus=='冻结'?'启用':'冻结'}'  WHERE staff_id=${req.body.staffID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM staff`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data)
                }
            });
        }
    });
};
// 员工管理删除
exports.stafflistsdel = (req, res) => {
    var sql =
        `delete from staff WHERE staff_id=${req.body.staffID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM staff`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data)
                }
            });
        }
    });
};
// 员工管理批量修改部门
exports.stafflistschangeall = (req, res) => {
    req.body.tableDatas.forEach(item=>{
        console.log(item.staff_id)
        var sql =
            `UPDATE staff SET department='${req.body.partment}',position='${req.body.selectedCity}' WHERE staff_id=${item.staff_id}`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }else{
                
            }
        });
    })
    var sql =
        `SELECT * FROM staff`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data)
        }
    });
};
// 员工管理名字搜索
exports.stafflistssearch = (req, res) => {
    var sql =
        `SELECT * FROM staff WHERE name LIKE '%${req.body.value}%'`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 员工管理部门搜索
exports.staffpartersearch = (req, res) => {
    var sql =
        `SELECT * FROM staff WHERE department='${req.body.selectedProvince}' and position='${req.body.selectedCity}'`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 员工管理列表按字母排序
exports.staffpartersort = (req, res) => {
    var sql =
        `SELECT * FROM staff ORDER BY name DESC`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};






// 组织架构
exports.departmentlists = (req, res) => {
    var sql =
        `SELECT * FROM department`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 组织架构添加部门
exports.departmentlistsinsert = (req, res) => {
    var sql =
        `INSERT INTO department(DepartmentName,Numbermembers,responsibleofficer,DepartmentDescription,Enable,edit,del)VALUES('${req.body.DepartmentName}',${req.body.Numbermembers},'${req.body.responsibleofficer}','${req.body.DepartmentDescription==''?'暂无相关描述':req.body.DepartmentDescription}','success','编辑','删除')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM department`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 组织架构部门删除
exports.departmentlistsdel = (req, res) => {
    var sql =
        `delete from department WHERE department_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM department`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data)
                }
            });
        }
    });
};
// 组织架构部门编辑
exports.departmentlistsupdate = (req, res) => {
    var sql =
        `UPDATE department SET DepartmentName='${req.body.DepartmentName}',Numbermembers=${req.body.Numbermembers},responsibleofficer='${req.body.responsibleofficer}',DepartmentDescription='${req.body.DepartmentDescription==''?'暂无相关描述':req.body.DepartmentDescription}' WHERE department_id=${req.body.department_id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM department`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data)
                }
            });
        }
    });
};

// 角色管理列表
exports.roleslists = (req, res) => {
    var sql =
        `SELECT * FROM roles`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 角色管理列表新增
exports.roleslistsinsert = (req, res) => {
    var sql =
        `INSERT INTO roles(roalesname,Numbermembers,describes,adds,setup,del)VALUES('${req.body.name}',30,'暂无相关描述','添加人员','设置权限','删除')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM roles`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 角色管理删除
exports.roleslistsdel = (req, res) => {
    var sql =
        `DELETE FROM roles WHERE Role_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM roles`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 角色管理查询
exports.roleslistsserch = (req, res) => {
    var sql =
        `SELECT * FROM roles where roalesname='${req.body.name}'`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};